package com.anhuanjia.tools.eao.controller;

import com.anhuanjia.bean.MhBranch;
import com.anhuanjia.bean.MhResource;
import com.efastsoft.bean.EcMember;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.extend.ExtendException;
import com.efastsoft.framework.orm.DAOSet;
import com.efastsoft.framework.utility.LogUtil;
import com.efastsoft.member.point.AfterMemberDelete;

public class DeleteMember extends AfterMemberDelete {

	@Override
	public boolean isUsable() {
		// TODO Auto-generated method stub
		return true;
	}
	
	/**
	 * 删除会员之后的扩展点
	 * 会员管理模块 会员列表，删除会员操作
	 */
	@Override
	public void execute(DAOSet<EcMember> members) throws ExtendException {
		
		DAOSet<MhBranch> branchDel = new DAOSet<>();
		DAOSet<MhResource> resourceDel = new DAOSet<>();
		
		for(EcMember member:members){
			long id = member.getID() ;
			//机构下的部门删除
			DataTable branchTable = new Q(" select * from mh_branch where Company = ?",id).fetch() ;
			if(branchTable.getRowCount()>0){
				for(DataRow branchRow : branchTable){
					MhBranch branch = new MhBranch() ;
					branch.setValue(branchRow); 
					branchDel.add(branch) ;
				}
			}
			//机构下的人员删除
			DataTable resourceTable = new Q(" select * from mh_resource where Company = ?",id).fetch() ;
			if(resourceTable.getRowCount()>0){
				for(DataRow resourceRow : resourceTable){
					MhResource resource = new MhResource() ;
					resource.setValue(resourceRow); 
					resourceDel.add(resource) ;
				}
			}
		}
		
		Transaction tran  = new Transaction();
		tran.delete(branchDel);
		tran.delete(resourceDel);
		if (tran.commit()){
			LogUtil.info("删除EAO模块的记录成功！");			
		} else {
			LogUtil.warn("删除EAO模块的记录失败！");
			
		}
	}
	
}
